using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSBusiness.InterFace;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMS.AdminTemplate
{
    [Function("Admin.ReportCategoryInfo")]
    public partial class ReportCategoryDetail : PageBase
    {
        private int CurrentReportCategoryID
        {
            get
            {
                if (ViewState["CurrentReportCategoryID"] == null)
                {
                    ViewState["CurrentReportCategoryID"]=GetIntByParameter("ReportCategoryID");
                }
                return (int)ViewState["CurrentReportCategoryID"];
            }
            set
            {
                ViewState["CurrentReportCategoryID"] = value;
            }
        }




        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDefault();
                LoadDetail();
            }
        }

        private void BindDefault()
        {
            ControlHelper.BindPublicItemsByTypeID(dplReportType, DictionaryCategoryConst.ReportTypeID, EmptyType.None);

            ControlHelper.BindTemplateList(chkTemplateListSend, EmptyType.None);
            ControlHelper.BindTemplateList(chkTemplateListDelete, EmptyType.None);
            ControlHelper.BindUserList(dplApproveUser, EmptyType.None);

            ControlHelper.BindPublicItemsByTypeID(listButtonEventSend,DictionaryCategoryConst.ButtonPermissionID, EmptyType.None);
            ControlHelper.BindPublicItemsByTypeID(listButtonEventDelete, DictionaryCategoryConst.ButtonPermissionID, EmptyType.None);
        }

        private void LoadDetail()
        {
            if (CurrentReportCategoryID > 0)
            {
                var entity = new ReportCategoryEntity(CurrentReportCategoryID);
                if (!entity.IsNew)
                {
                    lblReportID.Text = entity.ReportCategoryId.ToString();
                    txtReportName.Text = entity.ReportCategoryName;
                    dplReportType.SelectedValue = entity.ReportCategoryType.ToString();
                    txtCreationUrl.Text = entity.CreationUrl;
                    txtViewUrl.Text = entity.ViewUrl;
                    dplApproveUser.SelectedValue = entity.ApproveUserId.ToString();

                    //foreach (var reportToTemplate in entity.ReportCategoryToTemplate)
                    //{
                    
                    //    foreach (ListItem item in chkTemplateList.Items)
                    //    {
                    //        if(item.Value==reportToTemplate.TemplateId.ToString())
                    //        {
                    //            item.Selected = true;
                    //            break;
                    //        }
                    //    }
                    //}
                }
            }
        }

        private bool SaveDetail()
        {
            var entity = new ReportCategoryEntity(CurrentReportCategoryID);
            entity.ReportCategoryName = txtReportName.Text.Trim();
            entity.ReportCategoryType = int.Parse(dplReportType.SelectedValue);
            entity.CreationUrl = txtCreationUrl.Text.Trim();
            entity.ViewUrl = txtViewUrl.Text.Trim();
            entity.ApproveUserId = int.Parse(dplApproveUser.SelectedValue);
            if(entity.Save())
            {
                CurrentReportCategoryID = entity.ReportCategoryId;
                return true;
            }
            return false;
        }

        protected void btnSaveDetail_Click(object sender, EventArgs e)
        {
            if (SaveDetail())
            {
                Alert(GenerateAlert.SavSuccess);
               // RediretResolveUrl("~/admintemplate/reportCategorylist.aspx");
            }
            else
            {
                Alert(GenerateAlert.SaveFailed);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            RediretResolveUrl("~/AdminTemplate/ReportCategoryList.aspx");
        }

        protected void btnSaveButtonToTemplateSend_Click(object sender, EventArgs e)
        {
            var entity = new ReportCategoryEntity(CurrentReportCategoryID);
            if(!entity.IsNew)
            {
                if(listButtonEventSend.SelectedValue==string.Empty)
                {
                    Alert("Please select a button event.");
                    return;
                }
                var collection = new ReportCategoryToTemplateCollection();
                collection.DeleteMulti(ReportCategoryToTemplateFields.ReportCategoryId == entity.ReportCategoryId 
                    & ReportCategoryToTemplateFields.ButtonEventId==int.Parse(listButtonEventSend.SelectedValue)
                    & ReportCategoryToTemplateFields.ActionType==DictionaryCategoryConst.MessageActionType.SendMessage
                    );

                foreach (ListItem item in chkTemplateListSend.Items)
                {
                    if (item.Selected)
                    {
                        var reporCategoryToTemplate = new ReportCategoryToTemplateEntity
                        {
                            ReportCategoryId = entity.ReportCategoryId,
                            TemplateId = int.Parse(item.Value),
                            ButtonEventId = int.Parse(listButtonEventSend.SelectedValue),
                            ActionType=DictionaryCategoryConst.MessageActionType.SendMessage
                        };
                        reporCategoryToTemplate.Save();
                    }
                }
                Alert(GenerateAlert.SavSuccess);
            }
            else
            {
                Alert("Please save the Report Category Profile first.");
            }
        }

        protected void btnSaveButtonToTemplateDelete_Click(object sender, EventArgs e)
        {
            var entity = new ReportCategoryEntity(CurrentReportCategoryID);
            if (!entity.IsNew)
            {
                if (listButtonEventDelete.SelectedValue == string.Empty)
                {
                    Alert("Please select a button event.");
                    return;
                }
                var collection = new ReportCategoryToTemplateCollection();
                collection.DeleteMulti(ReportCategoryToTemplateFields.ReportCategoryId == entity.ReportCategoryId 
                    & ReportCategoryToTemplateFields.ButtonEventId == int.Parse(listButtonEventDelete.SelectedValue)
                    & ReportCategoryToTemplateFields.ActionType == DictionaryCategoryConst.MessageActionType.DeleteMessage);

                foreach (ListItem item in chkTemplateListDelete.Items)
                {
                    if (item.Selected)
                    {
                        var reporCategoryToTemplate = new ReportCategoryToTemplateEntity
                        {
                            ReportCategoryId = entity.ReportCategoryId,
                            TemplateId = int.Parse(item.Value),
                            ButtonEventId = int.Parse(listButtonEventDelete.SelectedValue),
                            ActionType = DictionaryCategoryConst.MessageActionType.DeleteMessage
                        };
                        reporCategoryToTemplate.Save();
                    }
                }
                Alert(GenerateAlert.SavSuccess);
            }
            else
            {
                Alert("Please save the Report Category Profile first.");
            }
        }

        protected void listButtonEventSend_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListItem item in chkTemplateListSend.Items)
            {
                item.Selected = false;
            }

            var collection = new ReportCategoryToTemplateCollection();
            collection.GetMulti(ReportCategoryToTemplateFields.ReportCategoryId == CurrentReportCategoryID
                & ReportCategoryToTemplateFields.ButtonEventId == int.Parse(listButtonEventSend.SelectedValue)
                & ReportCategoryToTemplateFields.ActionType == DictionaryCategoryConst.MessageActionType.SendMessage);


            foreach (var rptCategoryToTmp in collection)
            {
                foreach (ListItem item in chkTemplateListSend.Items)
                {
                    if (rptCategoryToTmp.TemplateId.ToString()==item.Value)
                    {
                        item.Selected = true;
                    }
                }
            }

        }

        protected void listButtonEventDelete_OnSelectedIndexChanged(object sender,EventArgs e)
        {
            foreach (ListItem item in chkTemplateListDelete.Items)
            {
                item.Selected = false;
            }

            var collection = new ReportCategoryToTemplateCollection();
            collection.GetMulti(ReportCategoryToTemplateFields.ReportCategoryId == CurrentReportCategoryID
                & ReportCategoryToTemplateFields.ButtonEventId == int.Parse(listButtonEventDelete.SelectedValue)
                & ReportCategoryToTemplateFields.ActionType == DictionaryCategoryConst.MessageActionType.DeleteMessage);


            foreach (var rptCategoryToTmp in collection)
            {
                foreach (ListItem item in chkTemplateListDelete.Items)
                {
                    if (rptCategoryToTmp.TemplateId.ToString() == item.Value)
                    {
                        item.Selected = true;
                    }
                }
            }
        }
    }
}
